//set main namespace
goog.provide('helloworld');



//get requirements
goog.require('lime.Director');
goog.require('lime.Scene');
goog.require('lime.RoundedRect');
goog.require('lime.Layer');
goog.require('lime.Circle');
goog.require('lime.Label');
goog.require('lime.animation.Spawn');
goog.require('lime.animation.FadeTo');
goog.require('lime.animation.KeyframeAnimation');
goog.require('lime.animation.ScaleTo');
goog.require('lime.animation.MoveTo');
goog.require('lime.transitions.SlideInRight');
goog.require('lime.Button');
goog.require('lime.GlossyButton');
//goog.require('lime.transition');
goog.require('lime.RoundedRect');
goog.require('lime.fill.Frame');

goog.require('goog.debug.Console');
goog.require('goog.math.Box');
goog.require('goog.storage.mechanism.HTML5LocalStorage');
goog.require('goog.net.XhrIo');
goog.require('goog.net.Jsonp');

//goog.require('GLL.Sprite');
//goog.require('GLL.Sprite.Anim');
goog.require('GLL.Button');

//goog.inherits(GLL.animation.KeyframeAnimation, lime.animation.KeyframeAnimation);
//goog.require('zlizer.Button');

function Warning(str)
{
	this.message = str;
	this.name = "Warning";
}

function getImg(img)
{
	return "IMG/" + img;
}

function GetVal(Val)
{
	Val.length;
}

function TEST_NET()
{
	var jsonp = new goog.net.Jsonp('http://127.0.0.1/test.php');
	jsonp.send(0);
}

// entrypoint
helloworld.start = function()
{
	TEST_NET();
	var rsltn_ipad = {w:1024, h:768}
	var rsltn_iphone = {w: 480, h:320}
	var rsltn_iphone4 = {w:960, h:640}
	
	var scr_h = rsltn_ipad.h;
	var scr_w = rsltn_ipad.w;

	var director = new lime.Director(document.body,scr_w, scr_h);
	director.makeMobileWebAppCapable();
	
	var scene = new lime.Scene(),
	    target = new lime.Layer().setPosition(512,384),
        circle = new lime.Circle().setSize(150,150).setFill(255,150,0),
        lbl = new lime.Label().setSize(160,50).setFontSize(30).setText('TOUCH ME!'),
        title = new lime.Label().setSize(800,70).setFontSize(60).setText('Now move me around!')
            .setOpacity(0).setPosition(512,80).setFontColor('#999').setFill(200,100,0,.1);
	//-------------------------------------------------------------------------------------
	
	var rect = new lime.Label().addTo(scene).setPosition(50, 50).setSize(100, 100).setFill(200, 0, 0);
	//scene.appendChild(rect);
	//scene.removeChild(rect);
	//rect.setHidden(true);
	
	var btn = new GLL.Button(getImg("Start.bmp"), function(e){
		e.target.setDisabled(true);
		console.log("btn clicked");
	}).setPosition(scr_w / 2, 100);
	
	scene.appendChild(btn);
	
	var anc_x = 61, anc_y = 18;
	//-------------------------------------------------------------------------------------
	var machine_x = 100;
	var machine_base = new lime.Sprite().setFill(getImg("machine_base.bmp")).setPosition(machine_x, scr_h / 2);
	//machine_base.setAnchorPoint(0, 0);
	scene.appendChild(machine_base);
	
	var machine_gun = new lime.Sprite().setFill(getImg("Gun.gif")).setAnchorPoint(0, 0);
	machine_gun.setPosition(0, 0).setRotation(45);
	machine_base.appendChild(machine_gun);
	
	var sto = new goog.storage.mechanism.HTML5LocalStorage();
	var val = sto.get("XX");

	goog.events.listen(machine_gun, ['mousedown','touchstart'], function(e)
	{
		var sto = new goog.storage.mechanism.HTML5LocalStorage();
		sto.set("XX", 123);
		var dragevent = new lime.events.Drag(e);
		goog.events.listen(dragevent, lime.events.Drag.Event.MOVE, function(e)
		{
			var pos = machine_gun.getPosition();
			console.log(pos);
		});
		
		//e.swallow(['mouseup','touchend'],function(){});
	
	});
	//-------------------------------------------------------------------------------------
	var drag_x = 10;
	var drag_cube = new lime.Sprite().setFill(getImg("controllor.bmp")).setPosition(drag_x, scr_h / 2);
	scene.appendChild(drag_cube);
	

	var ctrl_bnd = new goog.math.Box(10, drag_x, 768, drag_x);
	goog.events.listen(drag_cube, ['mousemove','touchstart'], function(e)
	{
		var dragevent = new lime.events.Drag(e);
		dragevent.setBounds(ctrl_bnd);
		
		goog.events.listen(dragevent, lime.events.Drag.Event.MOVE, function(e)
		{
			btn.setDisabled(false);
			var pos = drag_cube.getPosition();
			machine_base.setPosition(machine_x, pos.y);
			//pos.x
		});
		
	});
	

	//-------------------------------------------------------------------------------------
	var ppf = 124-47;
	var ppf_ = 0;
	//var footman = new GLL.New.animsprite('footman.gif', 420, 126, 3, 1).setPosition(200,200);
	//var footman = new GLL.animsprite.Anim('robot.bmp', 426, 94, 7).setPosition(50, 50);
	//footman.runAction(new lime.animation.MoveTo(512,10).setSpeed(1));
	//footman.setAnimSpeed(footman_distance, 100);
	
	//var footman = new GLL.Sprite().setPosition(50, 50)
	//.addAnim('walk', 'robot.bmp', 426, 94, 7, 1, ppf , 1);
	//footman.setAnim('walk');
	//footman._nowAnim.setSpeed(1);
	//footman.runAction(new lime.animation.MoveTo(850,50).setSpeed(0.5));
	
	//scene.appendChild(footman);
	//-------------------------------------------------------------------------------------

	var a1 = 5;
	var aa = a1 && 9;
	lbl.setText("" + aa);
	
	//add circle and label to target object
	target.appendChild(circle);
	target.appendChild(lbl);

	//add target and title to the scene
	scene.appendChild(target);
	scene.appendChild(title);


	// set current scene active
	director.replaceScene(scene);
	
	//add some interaction
	goog.events.listen(target,['mousedown','touchstart'], function(e)
	{
        //animate
        target.runAction(new lime.animation.Spawn(
            new lime.animation.FadeTo(.5).setDuration(.2),
            new lime.animation.ScaleTo(1.5).setDuration(.8)
        ));

        title.runAction(new lime.animation.FadeTo(1));

        //let target follow the mouse/finger
        e.startDrag();

        //listen for end event
        e.swallow(['mouseup','touchend'],function(){
		target.runAction(new lime.animation.Spawn(
		new lime.animation.FadeTo(1),
		new lime.animation.ScaleTo(1),
		new lime.animation.MoveTo(512,384)
            ));

            title.runAction(new lime.animation.FadeTo(0));
        });


    });

}


//this is required for outside access after code is compiled in ADVANCED_COMPILATIONS mode
goog.exportSymbol('helloworld.start', helloworld.start);
